package cn.dm.service.impl;

import cn.dm.client.RestDmImageClient;
import cn.dm.client.RestDmUserClient;
import cn.dm.common.Constants;
import cn.dm.common.EmptyUtils;
import cn.dm.common.MD5;
import cn.dm.common.RedisUtils;
import cn.dm.dto.Dto;
import cn.dm.dto.DtoUtil;
import cn.dm.pojo.DmImage;
import cn.dm.pojo.DmUser;
import cn.dm.service.LocalUserService;
import cn.dm.vo.DmUserVO;
import cn.dm.vo.TokenVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LocalUserServiceImpl implements LocalUserService {
    @Resource
    private RestDmUserClient restDmUserClient;
    @Resource
    private RestDmImageClient restDmImageClient;
    @Resource
    private RedisUtils redisUtils;
    @Override
    public Dto userLogin(String phone, String password) throws Exception {
        //查询用户对象
        Map<String, Object> param = new HashMap<>();
        param.put("phone", phone);
        param.put("password", password);
        List<DmUser> dmUserList = restDmUserClient.getDmUserListByMap(param);
        if (EmptyUtils.isEmpty(dmUserList)) {
            return DtoUtil.returnFail("账户密码错误", "1006");
        } else {
            DmUser dmUser = dmUserList.get(0);
            String userJson = (String) redisUtils.get(Constants.USER_TOKEN_PREFIX + dmUser.getId());
            if (EmptyUtils.isEmpty(userJson)) {
                //更新用户头像数
                Map<String, Object> imgParam = new HashMap<String, Object>();
                imgParam.put("targetId", dmUser.getId());
                imgParam.put("category", Constants.Image.ImageCategory.user);
                DmImage dmImage = restDmImageClient.getDmImageListByMap(imgParam).get(0);
                //封装返回参数
                DmUserVO dmUserV0 = new DmUserVO();
                BeanUtils.copyProperties(dmUser, dmUserV0);
                dmUserV0.setUserId(dmUser.getId());
                dmUserV0.setImageId(dmImage.getId());
                dmUserV0.setImgUrl(dmImage.getImgUrl());
                //生成登录验证票擀Token
                TokenVO tokenVO = createToken(dmUser);
                Object[] dto0bject = {dmUserV0, tokenVO};
                //把token信息保存Redi
                String userVOJSON=JSONObject.toJSONString(dmUserV0);
                redisUtils.set(dmUser.getId().toString(), Constants.Redis_Expire.SESSION_TIMEOUT, userVOJSON);
                redisUtils.set(Constants.USER_TOKEN_PREFIX + dmUser.getId(), Constants.Redis_Expire.SESSION_TIMEOUT, tokenVO.getToken());
                return DtoUtil.returnDataSuccess(dto0bject);
            } else {
                return DtoUtil.returnFail("用户已登录", "0001");

            }
        }
    }

    /**
     * 注销
     * @param token
     * @return
     * @throws Exception
     */
    @Override
    public Dto removeToken(String token) throws Exception {
        String qgUserJson=(String) redisUtils.get(token);
        DmUser qgUser= JSONObject.parseObject(qgUserJson,DmUser.class);
        redisUtils.delete(token);
        redisUtils.delete(String.valueOf(qgUser.getId()));
        return  DtoUtil.returnSuccess();

    }

    private TokenVO createToken(DmUser dmUser){
                String token = "DM-"+ MD5.getMd5(dmUser.getPhone()+dmUser.getId(),16);
                TokenVO tokenVo = new TokenVO();
                tokenVo.setToken(token);
                tokenVo.setExpTime(Constants.Redis_Expire.SESSION_TIMEOUT);
                tokenVo.setGenTime(new Date().getTime());
                return tokenVo;
            }

        }